package com.tywj.panda.driver.code

import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.huawei.hms.hmsscankit.ScanUtil
import com.huawei.hms.hmsscankit.WriterException
import com.huawei.hms.ml.scan.HmsBuildBitmapOption
import com.huawei.hms.ml.scan.HmsScan
import com.tywj.panda.customer.common.manager.AuthorityManager
import com.tywj.panda.customer.common.view.base.MyBaseActivity
import com.tywj.panda.driver.R
import com.tywj.panda.driver.route.DriverRoute
import com.tywj.lib.core.common.util.BarUtil
import com.tywj.lib.core.common.util.ResourcesUtil
import kotlinx.android.synthetic.main.driver_activity_check_code.*

/**
 * 验票码
 */
@Route(path = DriverRoute.DRIVER_CHECK_CODE)
class DriverQRCodeActivity : MyBaseActivity() {
    override fun getLayoutID(): Int = R.layout.driver_activity_check_code

    //生成的图片
    private var resultImage: Bitmap? = null

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        BarUtil.setStatusBarColor(this, ResourcesUtil.getColor(R.color.common_white))
        BarUtil.setStatusBarLightMode(this.window, true)
    }

    override fun initListener() {
        super.initListener()
    }


    override fun loadDataOnCreate() {
        super.loadDataOnCreate()

        generateCode()
    }

    //TODO:保持屏幕常亮
    private fun generateCode() {
        val width = 700
        val height = 700
        val content = AuthorityManager.uid
        val type = HmsScan.QRCODE_SCAN_TYPE
        val color = Color.BLACK
        val margin = 1
        val background = Color.WHITE

        try {
            //Generate the barcode.
            val options = HmsBuildBitmapOption.Creator().setBitmapMargin(margin).setBitmapColor(color)
                .setBitmapBackgroundColor(background).create()
            resultImage = ScanUtil.buildBitmap(content, type, width, height, options)
            barcodeImage.setImageBitmap(resultImage)
        } catch (e: WriterException) {
            Toast.makeText(this, "参数错误，二维码失败!", Toast.LENGTH_SHORT).show()
        }
    }
}